import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../model/alerm_info.dart';
import '../../../../utils/utils_logger.dart';
import '../../../../widgets/globel_widget.dart';
import '../../../../widgets/spirit/spirit_view.dart';
import '../detail/detail_view.dart';
import '../table_data.dart';
import 'group_all_logic.dart';

class GroupAllPage extends StatelessWidget {
  final logic = Get.put(GroupAllLogic());
  GroupAllPage({required this.data});
  /// 根据该数据条目生成组件
  final TabData data;

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle = TextStyle(color: Colors.yellow, fontSize: 50);
    return Obx(() => ListView.builder(
          itemCount: logic.liftList.length,
          itemBuilder: (context, index) {
            var data = logic.liftList[index];
            var alarmList = data.alermList;
            return Column(
              children: [
                spaceHeight(height: 20),
                SpiritComponent(
                  onTap: () {
                    logd('onTap');
                    Get.to(DetailPage());
                  },
                  onLongPress: () {
                    logd('onLongPress');
                  },
                  curStates: data.currentState,
                  lLeftText: data.liftName,
                  lRightText: data.currentAddr,
                  // rLeftText: getAlarmStr(alarmList, 0),
                  // rCenterText: getAlarmStr(alarmList, 1),
                  // rLeftColor: getAlarmColor(alarmList, 0),
                  // rCenterColor: getAlarmColor(alarmList, 1),
                  widgets: getWidgets(alarmList),
                ),
                spaceHeight(height: 20),
              ],
            );
          },
        ));
  }

  String getAlarmStr(List<AlermInfo> list, int index) {
    if (list.length > index) {
      return list[index].getTag();
    }
    return '';
  }

  Color getAlarmColor(List<AlermInfo> list, int index) {
    int isPush = 0;
    if (list.length > index) {
      isPush =  list[index].push;
    }
    return isPush == 0 ? Colors.white : Colors.orange;
  }
  
  getWidgets(List<AlermInfo> list) {

    var width = Get.width / 7;
    List<Widget> listWidget = [];
    // if (list.isNotEmpty) {
    //   var temp = list[0];
    //   temp.alerm = 'AL';
    //   temp.isPush = 0;
    //   list.add(temp);
    //   var temp2 = AlermInfo(1, 'alerm', 0);
    //   list.add(temp2);
    // }
    var size = list.length;

    for (int i = 0; i < size; i++) {
      var alarm = list[i];
      if (i != 3) {
        listWidget.add(
            Container(width: 0.5, height: 50, color: Colors.black54,));
      }
      Color color = alarm.getBgColor();
      listWidget.add(
          SizedBox(
          width :width, child: 
          Container(
          color: color, height: 50, child:
          Center(
          child: Text(alarm.getTag())))));


    }
    if (size < 3) {
      for (var i = size; i < 3; i++) {
        listWidget.add(
            Container(width: 0.5, height: 50, color: Colors.black54,));
        listWidget.add(
          SizedBox(
              width :width, child:
          Container(width: 0.5, height: 50, )));
      }
    }
    return listWidget;
  }
}